/*
*
*  文章评论数据模型
*  负责文章评论部分数据
*
*/
const db = require('../../config/db')
module.exports = {
    // 编写添加评论方法
    // 参数：article_id, comment_article_message,from_user_id,to_user_id,comment_create_time,comment_article_id
    add(article_id, comment_article_message, from_user_id, to_user_id, comment_create_time, comment_article_id) {
        return new Promise((resolve, reject) => {
            db.query("insert into db_comment_article(article_id,comment_article_message,from_user_id,to_user_id,comment_create_time,comment_article_id) values('" + article_id + "','" + comment_article_message + "','" + from_user_id + "','" + to_user_id + "','" + comment_create_time + "','" + comment_article_id + "')", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '添加评论失败。', err })
                } else {
                    resolve({ code: 200, msg: '添加评论成功。' })
                }
            })
        })
    },
    // 编写删除评论方法
    // 参数：comment_article_id
    remove(comment_article_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_comment_article where comment_article_id='" + comment_article_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询评论名称失败。' })
                } else {
                    db.query("delete from  db_comment_article where comment_article_id = '" + comment_article_id + "'", function (err, rows) {
                        if (err) {
                            resolve({ code: -1, msg: '删除评论失败。' })
                        } else {
                            resolve({ code: 200, msg: '删除评论成功。' })
                        }
                    })
                }
            })
        })
    },
    // 编写查询评论方法
    // 参数：article_id
    list(article_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_comment_article where article_id = '" + article_id + "' ", (err, rows) => {
                if (err) {
                    // 反馈信息给客户端
                    resolve({ code: -1, msg: '查询评论失败。' })
                } else {
                    resolve({ code: 200, msg: '查询评论成功。', result: rows })
                }
            })
        })
    },

    // 参数：article_id
    my_list(user_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_comment_article where from_user_id = '"+user_id+"'", (err, rows) => {
                if (err) {
                    // 反馈信息给客户端
                    resolve({ code: -1, msg: '查询评论失败。', err })
                } else {
                    resolve({ code: 200, msg: '获取我的评论。', result: rows })
                }
            })
        })
    },

    // 编写文章评论列表方法
    // 参数：page_num,page_size
    items(page_num, page_size) {
        return new Promise((resolve, reject) => {
            const params = [(parseInt(page_num) - 1) * parseInt(page_size), parseInt(page_size)]
            db.query("select * from db_comment_article limit " + params[0] + "," + params[1] + "", function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询文章评论失败。',
                        err
                    })
                } else {
                    //as更换名称
                    db.query('select count(*) as total from db_comment_article', function (error, among) {
                        if (error) {
                            resolve({
                                code: -1,
                                msg: '统计文章评论数量失败。',
                                error
                            })
                        } else {
                            resolve({
                                code: 200,
                                msg: "获取分页文章评论列表成功。",
                                result: rows,
                                page: {
                                    page_num: page_num,
                                    page_size: page_size,
                                    total: among[0]['total'] //查询表中的数量

                                }
                            })
                        }
                    })
                }
            })

        })
    },


    // 编写统计文章评论方法
    all() {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from db_comment_article', function (error, among) {
                if (error) {
                    resolve({
                        code: -1,
                        msg: '统计文章评论数量失败。',
                        error
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取文章评论总数成功。",
                        total: among[0]['total']
                    })
                }
            })

        })
    },

}